package org.feng.fxfront.pages.components;

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import lombok.Getter;
import org.feng.fxfront.util.ControlUtil;
import org.feng.fxfront.util.TimeUtil;

import java.time.LocalDateTime;

/**
 * 日志文本域组件
 *
 * @author feng
 */
@Getter
public class LogTextAreaComponent {

    private final TextArea textArea;
    private final VBox logBox;

    public LogTextAreaComponent(double height, double width) {
        // 控制台输入操作记录
        textArea = new TextArea();
        textArea.setScrollTop(Double.MAX_VALUE);
        textArea.setMaxHeight(height);
        textArea.setMaxWidth(width);
        textArea.setEditable(false);
        textArea.setStyle("-fx-text-fill: red");

        // 日志盒子
        logBox = new VBox();
        logBox.setMaxWidth(width);

        logBox.getChildren().add(new Label(""));

        Label operLogLabel = new Label("操作记录：");
        operLogLabel.setFont(Font.font(null, FontWeight.BOLD, 15));
        operLogLabel.setPadding(new Insets(5, 0, 5, 0));

        logBox.getChildren().add(operLogLabel);
        logBox.getChildren().add(textArea);

        // 清空日志记录按钮
        logBox.getChildren().add(new Label(""));
        Button clearLog = ControlUtil.genClearLogButton();
        logBox.getChildren().add(clearLog);
        clearLog.setOnMouseClicked(event -> {
            textArea.setText("");
        });
    }

    /**
     * 记录日志
     *
     * @param text 日志内容
     */
    public void log(String text) {
        textArea.appendText("【" + TimeUtil.parse(LocalDateTime.now()) + "】" + text + "\r\n");
    }
}
